package cn.doitedu.demos

import scala.collection.mutable.ListBuffer

object TestFind {

  def main(args: Array[String]): Unit = {

    val eventList = Array("e1","e1","e3","e5","e3","e6","e7","e6")
    /*val listBuffer = new ListBuffer[String]()
    val targetEventIndex = eventList.indexOf("e6")

    // 去重，留下相同事件中最近的一个
    for( i<- 0 until targetEventIndex){
      if(!listBuffer.contains(eventList(targetEventIndex-i-1))) listBuffer += eventList(targetEventIndex-i-1)
    }

    println(listBuffer)*/


    val buffer = cutEvents(eventList, "e6")
    for (elem <- buffer) {println(elem.toList)}


  }


  def cutEvents(arr:Array[String],ev:String) = {
    val indices = for ( i <- 0 until arr.size) yield if(arr(i).equals(ev)) i else -1
    val idx = indices.filter(_ != -1)
    val buffer = new ListBuffer[Array[String]]

    var j = 0
    for (i <- idx) {
      buffer += arr.slice(j, i + 1)
      j = i+1
    }

    buffer
  }
}
